import svg
import life

def button(parent, text, callback):
    """
        Creates a simple DOM button
    """
    element = document.createElement("input")
    element.setAttribute("type", "button")
    element.addEventListener("click", callback, false)
    element.setAttribute("value", text)
    parent.appendChild(element)

center = document.createElement("center")
document.body.appendChild(center)
center.appendChild(document.createTextNode("Game of life"))
center.appendChild(document.createElement("br"))

vg = svg.Svg(center, 40*10, 40*10)
vg.bgColor(64, 64, 64)
vg.smoothEdges(0)

game = life.Game(vg, 40, 40)

menu = document.createElement("div")
center.appendChild(menu)
loading = document.createTextNode("Loading...")
menu.appendChild(loading)

def buildMenu():
    """
        Remove "loading..." text, replace with menu buttons
    """
    menu.removeChild(loading)
    butToggle = button(menu, "On/Off", lambda x: game.toggle())
    butStep = button(menu, "Step", lambda x: game.step())
    butClear = button(menu, "Clear", lambda x: game.clear())
    butRandomize = button(menu, "Randomize", lambda x: game.randomize())

game.onBuild(buildMenu)

